package com.tuanzi.loan.web.bean.statistics.check;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.service.statistics.ActivityParticipateStatService;
import com.tuanzi.loan.business.service.statistics.CheckUserStatService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.check.CheckUserStatRequest;
import com.tuanzi.loan.vo.response.check.CheckUserStatResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.statistics.CheckUserStatDataModel;
import com.tuanzi.server.loan_base.model.bean.common.CommAdvertise;
import com.tuanzi.server.loan_base.model.bean.common.CommPlatform;
import com.tuanzi.server.loan_base.model.db.CommProductChannel;
import com.tuanzi.server.loan_base.model.db.CommProductPrdid;
import com.tuanzi.server.loan_base.model.db.LoanProductInfo;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.tuanzi.loan.business.constants.DictionaryCode.NEW_COLLISION_API_ID;

/**
 * 撞库统计
 *
 * created by chuIllusions_tan 20180727
 */
@Debug
@Data
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class CheckUserStatBean extends JSFListingBean<CheckUserStatRequest,CheckUserStatResponse>{


    @Autowired
    private CheckUserStatService checkUserStatService;

    @Autowired
    ActivityParticipateStatService activityParticipateStatService;

    private StreamedContent xlsFile;

    private List<CommProductPrdid> productList;
    private List<CommProductChannel> channelList;
    private List<CommPlatform> platformList;
    private List<CommAdvertise> adList;
    private List<LoanProductInfo> activityList;
    private List<FilterType> typeList;

    public StreamedContent downloadExcel() throws UnsupportedEncodingException {
        InputStream input = new ByteArrayInputStream(checkUserStatService.getExcel($).getBytes("GBK"));
        String fileName = System.currentTimeMillis() + ".csv";
        xlsFile = new DefaultStreamedContent(input, "application/vnd.ms-excel;charset=gb2312", fileName);
        return xlsFile;
    }

    @PostConstruct
    private void init() {
        initLazyDataModel();
        initList();
    }

    private void initList(){
        activityList = activityParticipateStatService.getActivity();
        adList = activityParticipateStatService.getAD();
        channelList = activityParticipateStatService.getChannel();
        platformList = activityParticipateStatService.getPlatform();
        productList = activityParticipateStatService.getProduct();
        typeList = new ArrayList<>();
        typeList.add(new FilterType("list_type","列表过滤"));
        typeList.add(new FilterType("detail_type","详情过滤"));
        typeList.add(new FilterType("-1000","全部"));
    }


    private void initLazyDataModel() {
        $ = new CheckUserStatRequest();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        // 初始化默认当前日期
        Date date = null;
        try {
            date = sdf.parse(sdf.format(new Date()));
            $.setStartDate(date);
            $.setEndDate(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        // 初始化默认选择全部
        List<Integer> defaultInt = new ArrayList<Integer>();
        defaultInt.add(-1000);
        $.setProducts(defaultInt);
        $.setChannels(defaultInt);


        List<String> defaultPlatForm = new ArrayList<String>();
        defaultPlatForm.add("-1000");
        $.setPlatforms(defaultPlatForm);

        List<String> defaultType = new ArrayList<String>();
        defaultType.add("-1000");
        $.setTypes(defaultType);

        List<Long> defaultActivity = new ArrayList<Long>();
        defaultActivity.add(-1000L);
        $.setActivities(defaultActivity);

        lazyDataModel = new CheckUserStatDataModel($);
    }

    public static class FilterType{
        @Getter private String code;
        @Getter private String name;

        public FilterType(String code,String name){
            this.code = code;
            this.name = name;
        }
    }

    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(NEW_COLLISION_API_ID);
    }
}
